package com.net.shop.car.manager.api.volley;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.text.TextUtils;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.net.shop.car.manager.App;
import com.net.shop.car.manager.utils.LogUtils;
import com.net.shop.car.manager.utils.NetworkUtils;

public class VolleyCenter {
	
	/** 网络请求队列 */
	private RequestQueue requestQueue;
	private Context context;
	private Gson gson = new Gson();
	
	private String baseUrl = "http://tapi.clejw.com/mobile/interface.do?";

	private VolleyCenter() {
		
	}
	
	public static VolleyCenter getVolley() {
		return InstanceHolder.volleyCenter;
	}
	
	/**
	 * @param context Context
	 */
	public void init(Context context) {
		this.context = context;
		requestQueue = Volley.newRequestQueue(context);
	}
	
	private static class InstanceHolder {
		public static VolleyCenter volleyCenter;
		static {
			volleyCenter = new VolleyCenter();
		}
	}
	
	/**
	 * @param request Request
	 */
	/**
	 * @param request Request
	 */
	public void addPostRequest(Request request,VolleyListener volleyListener) {
		if (requestQueue == null) {
			return;
		}
		if(NetworkUtils.isDisconnected(context)){
			App.i().showToast("网络连接异常");
			return;
		}
		StringBuffer urlBuffer = new StringBuffer(baseUrl);
		urlBuffer.append("content=");
		
		try {
			String content = URLEncoder.encode(request.toJSON().toString(), "utf-8");
			urlBuffer.append(content);
		} catch (UnsupportedEncodingException e) {
			LogUtils.e(e.getMessage());
		}
		String url = urlBuffer.toString();
		VolleyRequest volleyRequest = new VolleyRequest(Method.POST, url, volleyListener);
		requestQueue.add(volleyRequest);
	}
	
	/**
	 * @param request Request
	 */
	public void addGetRequest(Request request,VolleyListener volleyListener) {
		if (requestQueue == null) {
			return;
		}
		if(NetworkUtils.isDisconnected(context)){
			App.i().showToast("网络连接异常");
			return;
		}
		StringBuffer urlBuffer = new StringBuffer(baseUrl);
		urlBuffer.append("content=");
		try {
			String content = URLEncoder.encode(request.toJSON().toString(), "utf-8");
			urlBuffer.append(content);
		} catch (UnsupportedEncodingException e) {
			LogUtils.e(e.getMessage());
		}
		String url = urlBuffer.toString();
		
		VolleyRequest volleyRequest = new VolleyRequest(Method.GET, url, volleyListener);
		requestQueue.add(volleyRequest);
	}
	/**
	 * @param request
	 * @param volleyListener
	 */
	public void addPostRequestUseHead(Request request,
			final VolleyListener volleyListener) {
		JSONObject object = request.toJSON();
		final Map<String, String> map = new HashMap<String, String>();
		map.put("content", object.toString());

		StringRequest stringRequest = new StringRequest(Method.POST,
				"http://tapi.clejw.com/mobile/interface.do",
				new Listener<String>() {
					@Override
					public void onResponse(final String response) {
						volleyListener.onResponse();
					}
				}, new ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						LogUtils.e(error.getMessage());
						volleyListener.onError(error);

					}
				}) {
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				return map;
			}

			@Override
			protected com.android.volley.Response<String> parseNetworkResponse(
					NetworkResponse response) {
				com.android.volley.Response<String> strResponse = super
						.parseNetworkResponse(response);
				volleyListener.onParse(strResponse.result);
				return strResponse;
			}
		};
		requestQueue.add(stringRequest);
	}

	
	/**
	 * 图片请求
	 * @param url
	 * @param listener
	 * @param errorListener
	 */
	public void addImageRequest(String url,Listener<Bitmap> listener,ErrorListener errorListener) {
		if(NetworkUtils.isDisconnected(context)){
			App.i().showToast("网络连接异常");
			return;
		}
		if (TextUtils.isEmpty(url)) {
			errorListener.onErrorResponse(new VolleyError("错误图片链接"));
			return;
		}
		ImageRequest imageRequest = new ImageRequest(url, listener, 0, 0, Config.ARGB_8888, errorListener);
		requestQueue.add(imageRequest);
	}
}
